import Vue from 'vue'
import VueRouter from 'vue-router'
import MenuLayout from '@/layout/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'common',
    component: MenuLayout,
    redirect: '/home',
    children: []
  },
  {
    path: '/login',
    component: () => import('@/views/Login.vue')
  }
]

const router = new VueRouter({
  base: process.env.BASE_URL,
  mode: 'history',
  routes
})

export default router

// 动态添加路由的函数
export function dynamicAddRoutes(list) {
  list.map(item => {
    const {componentPath, layout, name, path, title, parent, breadcrumb = true } = item
    const component = () => import(`@/${componentPath}.vue`)
    const config = { path, name, component, meta: { title, breadcrumb }}
    if (layout) {
      router.addRoute(layout, config)
    } else if (parent) {
      router.addRoute(parent.name, config)
    } else {
      router.addRoute(config)
    }
  })
}